import request from '@/axios';
import {baseUrl} from '@/config/env';


/**
 * 常用CRUD
 */
export default {
    // 发送get请求
    getData(path, param = {}, headers = {}) {

        return new Promise((resolve, reject) => {
            request({
                url: baseUrl + path,
                method: 'get',
                params: param,
                headers: {

                    ...headers
                },
            })
                .then((res) => {
                    return resolve(res.data);
                })
                .catch(reject);
        });

    },
    // 发送post请求
    postData(path, param = {}, headers = {}) {
        return new Promise((resolve, reject) => {
            request({
                url: baseUrl + path,
                method: 'post',
                params: param,
                headers: {

                    ...headers
                },
            })
                .then((res) => {
                    return resolve(res.data);
                })
                .catch(reject);
        });
    },
// 发送post请求
    postJsonData(path, param = {}, headers = {}) {
        return new Promise((resolve, reject) => {
            request({
                url: baseUrl + path,
                method: 'post',
                data: JSON.stringify(param),
                headers: {
                    "Content-Type": "application/json;charset=UTF-8",
                    ...headers
                },
            })
                .then((res) => {
                    return resolve(res.data);
                })
                .catch(reject);
        });


    },
    // 发送put请求
    putData(path, param = {}, headers = {}) {
        return new Promise((resolve, reject) => {
            request({
                url: baseUrl + path,
                method: 'put',
                data: JSON.stringify(param),
                headers: {
                    "Content-Type": "application/json;charset=UTF-8",
                    ...headers
                },
            })
                .then((res) => {
                    return resolve(res.data);
                })
                .catch(reject);
        });


    },
    deleteData(path, param = {}, headers = {}) {
        return new Promise((resolve, reject) => {
            request({
                url: baseUrl + path,
                method: 'DELETE',
                data: JSON.stringify(param),
                headers: {
                    "Content-Type": "application/json;charset=UTF-8",
                    ...headers
                },
            })
                .then((res) => {
                    return resolve(res.data);
                })
                .catch(reject);
        });

    }


}






